local cjson = require 'cjson'
ngx.req.read_body();
local args = ngx.req.get_uri_args()
local logData = {
    product = args.product,
    imei = args.imei,
    srcVersion = args.old_version,
    dstVersion = args.new_version,
    autoDownload = tonumber(args.auto) or -1,
    respServer = ngx.var.host,
    reqServer = ngx.var.remote_addr
}
ngx.req.set_header("Content-Type", "application/json")
local res = ngx.location.capture('/fota-version/record',
    {
        method = ngx.HTTP_POST,
        body = cjson.encode(logData)
    })

if tonumber(res.status) ~= ngx.HTTP_OK then
    ngx.header.content_type = 'application/json'
    ngx.log(ngx.ERR, "write log err:", cjson.encode(res))
    ngx.exit(res.status or ngx.HTTP_INTERNAL_SERVER_ERROR)
else
    local body = cjson.decode(res.body)
    if tonumber(body.status) ~= 200 then
        ngx.log(ngx.ERR, "fota version server write log err:", cjson.encode(res))
        ngx.exit(ngx.HTTP_BAD_REQUEST)
    end
end
